// Copyright (C) ZHENG Zhong <heavyzheng nospam-at gmail D0T com>
//
// $Id: logger.hpp 287 2008-11-27 14:32:14Z heavyzheng $
//

#ifndef ZZHENG_LOGGING_LOG4CPLUS_LOGGER_HPP_20080901__
#define ZZHENG_LOGGING_LOG4CPLUS_LOGGER_HPP_20080901__

#include <zzheng/logging/log4cplus/config.hpp>

#if defined(ZZHENG_LOGGING_USES_LOG4CPLUS) || defined(ZZHENG_LOGGING_HAS_LOG4CPLUS)


#include <zzheng/logging/log4cplus/include_logger.hpp>
#include <zzheng/logging/log_level.hpp>

#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <string>
#include <map>

namespace zzheng {
namespace logging {
namespace log4cplus_ {

    class ZZHENG_LOGGING_DECL log_factory; // forward declaration.

    //! The logger implementation class based on log4cplus::Logger.
    class ZZHENG_LOGGING_DECL logger: private boost::noncopyable {

        typedef std::map<std::string, boost::shared_ptr<logger> > child_map;

        friend log_factory;

    public:

        // Use auto-generated destructor.

        //! Returns the name of this logger.
        //! \return the name of this logger.
        std::string name() const;

        //! Returns the threshold of this logger.
        //! \return the threshold of this logger.
        log_level threshold() const;

        //! Sets the threshold to this logger.
        //! \param threshold  the new threshold to set.
        void set_threshold(const log_level& threshold);

        //! Returns a child logger object by name. If the requested child logger does not exist,
        //! this function will create a new one.
        //! \param child_name  the name of the child logger.
        //! \return the requested child logger.
        boost::weak_ptr<logger> child(const std::string& child_name);

        //! Logs a message to this logger at the specified log level.
        //! \param level  the log level of the message.
        //! \param msg    the message to log.
        void log(const log_level& level, const std::string& msg);

    private:

        //! Constructs a logger to wrap the log4cplus root logger object.
        //! \param hierarchy  the log4cplus hierarchy from which the root logger is retrieved.
        //! \param threshold  the threshold of the root logger.
        explicit logger(log4cplus::Hierarchy& hierarchy, const log_level& threshold);

        //! Constructs a logger to wrap a log4cplus logger object.
        //! \param hierarchy  the log4cplus hierarchy to which the underlying logger is attached.
        //! \param name       the name of the logger.
        //! \param threshold  the threshold of the logger.
        explicit logger(log4cplus::Hierarchy& hierarchy,
                        const std::string& name,
                        const log_level& threshold);

    private:

        log4cplus::Logger impl_;     //!< The underlying log4cplus logger object.
        child_map         children_; //!< The child logger objects.

    }; // class logger


} // namespace zzheng::logging::log4cplus_
} // namespace zzheng::logging
} // namespace zzheng


#endif // ZZHENG_LOGGING_USES_LOG4CPLUS || ZZHENG_LOGGING_HAS_LOG4CPLUS

#endif // ZZHENG_LOGGING_LOG4CPLUS_LOGGER_HPP_20080901__



